package com.android.hidephotosplus.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.hidephotosplus.R;
import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMediaOfAlbum;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.MLoadMedia;
import com.android.hidephotosplus.util.RetainCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class AlbumAdapter extends ArrayAdapter<ModelAlbum> {

	private Context context;

	private ViewHolder viewHolder;

	private ImageLoader imageLoader;

	private MLoadMedia loadMedia;

	private int widthAlbum;

	private int heithAlbum;

	public boolean scroolState = false;

	private boolean isPublic;

	public boolean isScroolState() {
		return scroolState;
	}

	public void setScroolState(boolean scroolState) {
		this.scroolState = scroolState;
	}

	public AlbumAdapter(Context context, ArrayList<ModelAlbum> objects,
			boolean isPublic) {
		super(context, R.layout.item_album_new, objects);
		this.context = context;
		this.isPublic = isPublic;

		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));

		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();

		widthAlbum = display.getWidth()
				/ context.getResources().getInteger(
						R.integer.number_columns_album) - 15;
		heithAlbum = widthAlbum + 10;
		RetainCache c = RetainCache.getOrCreateRetainableCache();
		loadMedia = c.loadMedia;
		if (loadMedia == null) {
			loadMedia = new MLoadMedia(Config.onSize(context),
					Config.MAX_PIXELS_WIDTH, Config.MAX_PIXELS_HEIGHT);
			c.loadMedia = loadMedia;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.item_album_new, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.image = (ImageView) view.findViewById(R.id.image);
			viewHolder.imagePlay = (ImageView) view
					.findViewById(R.id.imagePlay);
			viewHolder.imagePlay2 = (ImageView) view
					.findViewById(R.id.imagePlay2);
			viewHolder.imagePlay3 = (ImageView) view
					.findViewById(R.id.imagePlay3);
			viewHolder.imagePlay4 = (ImageView) view
					.findViewById(R.id.imagePlay4);

			viewHolder.image2 = (ImageView) view.findViewById(R.id.image2);

			viewHolder.image3 = (ImageView) view.findViewById(R.id.image3);

			viewHolder.image4 = (ImageView) view.findViewById(R.id.image4);

			viewHolder.textNameAlbum = (TextView) view
					.findViewById(R.id.textNameAlbum);
			viewHolder.imageCheck = (ImageView) view
					.findViewById(R.id.imageCheck);
			view.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) view.getTag();

		LayoutParams params = view.getLayoutParams();
		if (params != null) {
			params.height = heithAlbum;
			params.width = widthAlbum;
			view.setLayoutParams(params);
		}

		LayoutParams paramsPlay = viewHolder.imagePlay.getLayoutParams();
		if (paramsPlay != null) {
			paramsPlay.height = widthAlbum / 4;
			paramsPlay.width = widthAlbum / 4;
			viewHolder.imagePlay.setLayoutParams(paramsPlay);
			viewHolder.imagePlay2.setLayoutParams(paramsPlay);
			viewHolder.imagePlay3.setLayoutParams(paramsPlay);
			viewHolder.imagePlay4.setLayoutParams(paramsPlay);
		}

		ModelAlbum item = getItem(position);
		if (item != null) {
			viewHolder.textNameAlbum.setText(item.getNameAlbum() + " | "
					+ item.getNumberMedia());
			ModelMediaOfAlbum mediaOfAlbum = item.getMediaOfAlbum();
			if (mediaOfAlbum == null)
				mediaOfAlbum = new ModelMediaOfAlbum();
			if (isPublic) {
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo4(),
						mediaOfAlbum.getPathMediaStock4(), viewHolder.image,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo3(),
						mediaOfAlbum.getPathMediaStock3(), viewHolder.image2,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo2(),
						mediaOfAlbum.getPathMediaStock2(), viewHolder.image3,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo(),
						mediaOfAlbum.getPathMediaStock(), viewHolder.image4,
						isScroolState(), mNotifyRunnable, isPublic);

			} else {
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo4(),
						mediaOfAlbum.getPathMediaHidden4(), viewHolder.image,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo3(),
						mediaOfAlbum.getPathMediaHidden3(), viewHolder.image2,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo2(),
						mediaOfAlbum.getPathMediaHidden2(), viewHolder.image3,
						isScroolState(), mNotifyRunnable, isPublic);
				loadMedia.loadBitmap(context, mediaOfAlbum.isVideo(),
						mediaOfAlbum.getPathMediaHidden(), viewHolder.image4,
						isScroolState(), mNotifyRunnable, isPublic);
			}
			if (mediaOfAlbum.isVideo4()) {
				viewHolder.imagePlay.setVisibility(View.VISIBLE);
			} else
				viewHolder.imagePlay.setVisibility(View.GONE);
			if (mediaOfAlbum.isVideo3()) {
				viewHolder.imagePlay2.setVisibility(View.VISIBLE);
			} else
				viewHolder.imagePlay2.setVisibility(View.GONE);
			if (mediaOfAlbum.isVideo2()) {
				viewHolder.imagePlay3.setVisibility(View.VISIBLE);
			} else
				viewHolder.imagePlay3.setVisibility(View.GONE);
			if (mediaOfAlbum.isVideo()) {
				viewHolder.imagePlay4.setVisibility(View.VISIBLE);
			} else
				viewHolder.imagePlay4.setVisibility(View.GONE);

			if (item.isChecked)
				viewHolder.imageCheck.setVisibility(View.VISIBLE);
			else
				viewHolder.imageCheck.setVisibility(View.GONE);
		}

		return view;
	}

	private class ViewHolder {
		ImageView image;
		ImageView imagePlay;
		ImageView image2;
		ImageView image3;
		ImageView image4;
		ImageView imagePlay2;
		ImageView imagePlay3;
		ImageView imagePlay4;
		ImageView imageCheck;
		TextView textNameAlbum;
	}

	Runnable mNotifyRunnable = new Runnable() {
		@Override
		public void run() {
			notifyDataSetChanged();
		}
	};

}
